package com.teama4.shop.controller;

import com.teama4.shop.annotation.NeedLogin;
import com.teama4.shop.domain.DeliveryAddress;
import com.teama4.shop.domain.DeliveryAddressExample;
import com.teama4.shop.service.DeliveryAddressService;
import com.teama4.shop.utils.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
@Controller
@RequestMapping("/deliveryAddress")
public class DeliveryAddressController {
    @Autowired
    DeliveryAddressService deliveryAddressService;

    /**
     * 删除收货地址
     *
     * @param id 收货地址的id
     */
    @NeedLogin
    @RequestMapping("delDeliveryAddress")
    public void delDeliveryAddress(HttpServletResponse resp, HttpServletRequest req, Integer id) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(false);
        if (session != null) {
            int res = deliveryAddressService.deleteByPrimaryKey(id);
            if (res > 0) {
                //如果删除成功
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "ok");
                JSONUtil.printByJSON(resp, map);
            } else {
                //如果删除失败
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "fail");
                JSONUtil.printByJSON(resp, map);
            }
        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }
    }

    /**
     * 删除收货地址
     *
     * @param id 收货地址的id
     */


    /**
     * 修改收货地址
     *
     * @param id       收货地址的id
     * @param receiver 收货人名称
     * @param province 省
     * @param city     城市
     * @param area     省/区
     * @param address  详细地址
     * @param phone    电话
     * @param postCode 邮编
     */
    @NeedLogin
    @RequestMapping("editDeliveryAddress")
    public void editDeliveryAddress(HttpServletResponse resp, HttpServletRequest req, Integer id, String receiver, String province, String city, String area, String address, String phone, String postCode) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(false);
        if (session != null) {
            DeliveryAddress da = new DeliveryAddress();
            da.setId(id);
            da.setReceiver(receiver);
            da.setProvince(province);
            da.setCity(city);
            da.setArea(area);
            da.setAddress(address);
            da.setPhone(phone);
            if (postCode != null && !postCode.equals("")) {
                da.setPostcode(postCode);
            }
            int res = deliveryAddressService.updateByPrimaryKeySelective(da);
            if (res > 0) {
                //如果更新成功
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "ok");
                JSONUtil.printByJSON(resp, map);
            } else {
                //如果更新失败
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "fail");
                JSONUtil.printByJSON(resp, map);
            }
        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }
    }

    /**
     * 增加收货地址
     *
     * @param receiver 收货人名称
     * @param province 省
     * @param city     城市
     * @param area     县/区
     * @param address  详细地址
     * @param phone    电话
     * @param postCode 邮编
     */
    @NeedLogin
    @RequestMapping("addDeliveryAddress")
    public void addDeliveryAddress(HttpServletResponse resp, HttpServletRequest req, String receiver, String province, String city, String area, String address, String phone, String postCode) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(false);
        if (session != null && session.getAttribute("userId") != null) {
            Integer userId = (Integer) session.getAttribute("userId");
            DeliveryAddress da = new DeliveryAddress();
            da.setReceiver(receiver);
            da.setProvince(province);
            da.setCity(city);
            da.setArea(area);
            da.setAddress(address);
            da.setPhone(phone);
            da.setUserId(userId);

            DeliveryAddressExample deliveryAddressExample = new DeliveryAddressExample();
            deliveryAddressExample.createCriteria().andUserIdEqualTo(userId);
            if(deliveryAddressService.selectByExample(deliveryAddressExample).size()>0){
                //如果存在地址
                da.setIsdefault(-1);
            }else {
                da.setIsdefault(1);
            }
            if (postCode != null && !postCode.equals("")) {
                da.setPostcode(postCode);
            }
            int insResult = deliveryAddressService.insertSelective(da);
            if (insResult > 0) {
                //如果插入成功
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "ok");
                JSONUtil.printByJSON(resp, map);
            } else {
                //如果插入失败
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "fail");
                JSONUtil.printByJSON(resp, map);
            }

        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }
    }

    @NeedLogin
    @RequestMapping("setDefault")
    public void setDefault(HttpServletResponse resp, HttpServletRequest req,Integer deliveryAddressId) throws UnsupportedEncodingException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession(false);
        if (session != null && session.getAttribute("userId") != null) {
            Integer userId = (Integer) session.getAttribute("userId");
            //将选中的改为1
            DeliveryAddress deliveryAddress1 = new DeliveryAddress();
            deliveryAddress1.setId(deliveryAddressId);
            deliveryAddress1.setIsdefault(1);
            int res = deliveryAddressService.updateByPrimaryKeySelective(deliveryAddress1);

            //将其他的改为-1
            DeliveryAddress deliveryAddress = new DeliveryAddress();
            deliveryAddress.setIsdefault(-1);

            DeliveryAddressExample deliveryAddressExample = new DeliveryAddressExample();
            DeliveryAddressExample.Criteria criteria = deliveryAddressExample.createCriteria();
            criteria.andIdNotEqualTo(deliveryAddressId);
            criteria.andUserIdEqualTo(userId);

            int insResult = deliveryAddressService.updateByExampleSelective(deliveryAddress,deliveryAddressExample);
            if (insResult > 0&&res>0) {
                //如果插入成功
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "ok");
                JSONUtil.printByJSON(resp, map);
            } else {
                //如果插入失败
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("result", "fail");
                JSONUtil.printByJSON(resp, map);
            }

        } else {
            //如果session获取不到值
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("result", "noSession");
            JSONUtil.printByJSON(resp, map);
        }
    }

}
